<template>
  <el-dialog
    title="注册"
    :visible.sync="visible"
    width="450px"
    :close-on-click-modal="false"
    center   @close="handleClose"
    class="register-panel"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="registerForm"
      label-width="100px"
    >
      <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="form.idNumber"
          placeholder="请输入身份证号"
          maxlength="18"
        ></el-input>
      </el-form-item>

      <el-form-item label="组织架构" prop="organization">
        <el-select
          v-model="form.organization"
          placeholder="请选择组织架构"
          style="width: 100%"
        >
          <el-option label="组织1" value="org1"></el-option>
          <el-option label="组织2" value="org2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用户类型" prop="userType">
        <el-select
          v-model="form.userType"
          placeholder="请选择用户类型"
          style="width: 100%"
        >
          <el-option label="教员" value="1"></el-option>
          <el-option label="学员" value="2"></el-option>
        </el-select>
      </el-form-item>

      <template v-if="form.userType === '2'">
        <el-form-item label="年级" prop="grade">
          <el-select
            v-model="form.grade"
            placeholder="请选择年级"
            style="width: 100%"
          >
            <el-option label="2023级" value="2023"></el-option>
            <el-option label="2022级" value="2022"></el-option>
            <el-option label="2021级" value="2021"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="专业" prop="major">
          <el-input
            v-model="form.major"
            placeholder="请输入专业"
          ></el-input>
        </el-form-item>
      </template>

      <el-form-item label="学院邮箱" prop="email">
        <el-input
          v-model="form.email"
          placeholder="请输入学院邮箱"
        ></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'RegisterDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    // 身份证号验证
    const validateIdCard = (rule, value, callback) => {
      const reg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
      if (!value) {
        callback(new Error('请输入身份证号'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入正确的身份证号'))
      } else {
        callback()
      }
    }

    // 邮箱验证
    const validateEmail = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入邮箱'))
      } else if (!value.endsWith('edu.cn')) {
        callback(new Error('请输入学院邮箱'))
      } else {
        callback()
      }
    }

    return {
      form: {
        idNumber: '',
        organization: '',
        userType: '',
        grade: '',
        major: '',
        email: ''
      },
      rules: {
        idNumber: [
          { required: true, validator: validateIdCard, trigger: 'blur' }
        ],
        organization: [
          { required: true, message: '请选择组织架构', trigger: 'change' }
        ],
        userType: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ],
        grade: [
          { required: true, message: '请选择年级', trigger: 'change' }
        ],
        major: [
          { required: true, message: '请输入专业', trigger: 'blur' }
        ],
        email: [
          { required: true, validator: validateEmail, trigger: 'blur' }
        ]
      }
    }
  },
  methods: { handleClose() {
      this.$emit('update:visible', false);
    },
    submitForm() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            // 这里调用注册接口
            const response = await this.$http.post('/api/register', this.form)
            if (response.data.code === 1) {
              this.$message.success('注册成功')
              this.$emit('update:visible', false)
              this.$emit('register-success')
            } else {
              this.$message.error(response.data.message || '注册失败')
            }
          } catch (error) {
            this.$message.error('注册失败，请稍后重试')
            console.error('Registration error:', error)
          }
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.$refs.registerForm.resetFields()
    }
  },
  watch: {
    visible(val) {
      if (!val) {
        this.resetForm()
      }
    },
    'form.userType'(val) {
      if (val !== '2') {
        this.form.grade = ''
        this.form.major = ''
      }
    }
  }
}
</script>

<style scoped>
.register-panel {
  //max-width: 600px;
}
.register-panel .el-input,
.register-panel .el-select {
  width: 100%;
}
.dialog-footer {
  text-align: center;
  width: 100%;
  display: block;
}
.dialog-footer .el-button {
  width: 120px;
}
</style>
